#include "staffserver.h"
void *threadFun(void *arg)
{
  DATA data;
  ssize_t bytes;
  MSG msg = *(MSG *)arg;
  while (1)
  {
    //接收数据
    if ((bytes = recv(msg.acceptfd, &data, sizeof(data), 0)) == -1)
    {
      ERRLOG("recv error");
    }
    else if (bytes == 0)
    {
      printf("客户端%s-%d退出了\n", inet_ntoa(msg.clientaddr.sin_addr), ntohs(msg.clientaddr.sin_port));
      pthread_exit(NULL);
    }
    if (strcmp("quit", data.quit) == 0)
    {
      printf("客户端%s-%d退出了\n", inet_ntoa(msg.clientaddr.sin_addr), ntohs(msg.clientaddr.sin_port));
      pthread_exit(NULL);
    }
    printf("flat=%d\n", data.flat);
    printf("name=%s\n", data.name);
    printf("password=%s\n", data.password);
    printf("id=%d\n", data.id);
    printf("wages=%d\n", data.wages);
    printf("dapartemet=%s\n", data.department);
    printf("gender=%s\n", data.gender);
    switch (data.flat)
    {
    case 1:
      do_Administrators_sign_in(data, msg);
      break;
    case 2:
      do_Administrators_add_to(data, msg);
      break;
    case 3:
      do_Administrators_delete_id(data, msg);
      break;
    case 4:
      do_Administrators_delete_name(data, msg);
      break;
    case 5:
      do_Administrators_modify(data, msg);
      break;
    case 6:
      do_Administrators_query(data, msg);
      break;
    case 7:
      do_Administrators_Historical_records(data, msg);
      break;
    case 8:
      do_Staff_register(data, msg);
      break;
    case 9:
      do_Staff_sign_in(data, msg);
      break;
    default:
      printf("选项错误,请重试\n");
      break;
    }
  }
}

int main()
{
  char sql[2 * N] = {0};
  char *errmsg;
  //创建数据库
  sqlite3 *db;
  if (sqlite3_open(STAFFDB, &db) != SQLITE_OK)
  {
    printf("错误：%s\n", sqlite3_errmsg(db));
  }
  else
  {
    printf("成功：数据库打开或者创建成功\n");
  }
  //创建员工用户表
  sprintf(sql, "create table if not exists %s(name char,age int,gender char,password char,id int primary key,department char,wages int);", EMPLOYEE);
  if (sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
  {

    printf("错误：%s\n", errmsg);
  }
  else
  {
    printf("成功：表%s创建成功了\n", EMPLOYEE);
  }
  //创建登录信息表
  sprintf(sql, "create table if not exists %s(name char,time char);", HISTOR);
  if (sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
  {
    printf("错误：%s\n", errmsg);
  }
  else
  {
    printf("成功：表%s创建成功了\n", HISTOR);
  }
  int sockfd, acceptfd;
  struct sockaddr_in serveraddr, clientaddr;
  socklen_t addrlen = sizeof(serveraddr);
  //创建套接字
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  {
    ERRLOG("socket error");
  }
  //填写网络结构体
  serveraddr.sin_family = AF_INET;
  serveraddr.sin_addr.s_addr = inet_addr("192.168.31.69");
  serveraddr.sin_port = htons(atoi("6666"));
  //将套接字与网络信息结构体进行连接
  if (bind(sockfd, (struct sockaddr *)&serveraddr, addrlen) == -1)
  {
    ERRLOG("bind error");
  }

  //将套接字设置为动态接听状态
  if (listen(sockfd, 5) == -1)
  {
    ERRLOG("listen error");
  }
  //使用setsockopt函数设置允许端口复用
  int on = 1;
  if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
  {
    ERRLOG("setsockopt error");
  }
  //使用多线程进行并发通信
  pthread_t thread;
  MSG msg;
  while (1)
  {
    //主线程执行accept负责链接客户端
    if ((acceptfd = accept(sockfd, (struct sockaddr *)&clientaddr, &addrlen)) == -1)
    {
      ERRLOG("accept error");
    }
    printf("客户端%s:%d连接了\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));
    //给结构体变量赋值
    msg.acceptfd = acceptfd;
    msg.clientaddr = clientaddr;
    msg.db = db;
    //如果有客户端链接,创建子线程处理函数里面与客户端通信
    if (pthread_create(&thread, NULL, threadFun, &msg) != 0)
    {
      ERRLOG("pthread_create error");
    }
    //将子线程设置为分态
    pthread_detach(thread);
  }
  return 0;
}